<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="100" sortable>
			</el-table-column>
			<el-table-column prop="dictionaryType.name" label="分类" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="180">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next, sizes" background
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :page-sizes="pageSizes"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/修改界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="分类明细" prop="username">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="分类">
          <el-select v-model="saveForm.dictionaryType" value-key="id" clearable placeholder="请选择">
            <!--
              key:唯一标识
              label:选中之后展示到选择框中的值
              value:选中之后绑定给模型层的值
            -->
            <el-option
                v-for="item in dictionaryTypes"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
        title:"",
				filters: {
          keyword: ''
				},
				dictionaryDetails: [],
        dictionaryTypes:[],
				total: 0,
				currentPage: 1,
        pageSize: 10,
        pageSizes: [5, 10, 15, 20],
				listLoading: false,
				sels: [],//列表选中行
				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
          name: [
						{ required: true, message: '请输入分类明细', trigger: 'blur' }
						]
				},
				//新增界面数据
				saveForm: {
          name:'',
          id:null,
          dictionaryType:{}
				}
			}
		},
		methods: {
      handleSizeChange(val){
        this.pageSize = val;
        this.getDictionaryDetails();
      },
      search(){
        this.currentPage = 1;
        this.getDictionaryDetails();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryDetails();
			},
			//获取员工列表
			getDictionaryDetails() {
				let para = {
					currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/dictionaryDetail/page",para)
          .then(result=>{
            console.log(result)
            this.listLoading = false;
            result = result.data;
            this.dictionaryDetails = result.rows;
            this.total = result.total;
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              type: 'error',
              message: '系统错误,我们正在殴打程序员'
            })
          })
			},
      getDictionaryTypes(){
        // 发送Ajax请求获取数据
        this.$http.get("/dictionaryType/list")
            .then(result=>{
              this.dictionaryTypes = result.data;
            })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dictionaryDetail/"+row.id)
            .then(result=>{
              this.listLoading = false;
              result = result.data;
              if (result.success){
                // 删除成功
                this.$message({
                  message: result.message,
                  type: 'success'
                });
                this.currentPage = 1;
                this.getDictionaryDetails();
              } else {
                // 删除失败
                this.$message({
                  message: result.message,
                  type: 'error'
                });
                this.currentPage = 1;
                this.getDictionaryDetails();
              }
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                type: 'error',
                message: '系统错误,我们正在殴打程序员'
              });
            })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = "编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.saveFormVisible = true;
				this.saveForm = {
          name:'',
          id:null,
          dictionaryType:{}
				};
			},
			//新增
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
                .then(() => {
                  this.saveLoading = true; // 开启按钮忙等
							    let para = Object.assign({}, this.saveForm); // 克隆表单数据
                  // 处理后端需要的数据格式
                  this.$http.post("/dictionaryDetail/save",para)
                      .then(result=>{
                        this.saveLoading = false;
                        result = result.data;
                        if (result.success){
                          // 新增成功
                          this.$message({
                            message: result.message,
                            type: 'success'
                          });
                          this.currentPage = 1;
                          this.getDictionaryDetails();
                          this.$refs['saveForm'].resetFields(); // 重置表单校验
                          this.saveFormVisible = false;
                        } else {
                          // 新增失败
                          this.$message({
                            message: result.message,
                            type: 'error'
                          });
                          this.currentPage = 1;
                          this.getDictionaryDetails();
                        }
                      })
                      .catch(result=>{
                        this.saveLoading = false;
                        this.$message({
                          type: 'error',
                          message: '系统错误,我们正在殴打程序员'
                        });
                      })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)//.toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
				  // 发送Ajax请求删除数据
          this.$http.patch("/dictionaryDetail",ids)
              .then(result=>{
                this.listLoading = false;
                result = result.data;
                if (result.success){
                  // 删除成功
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                  this.currentPage = 1;
                  this.getDictionaryDetails();
                } else {
                  // 删除失败
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                  this.currentPage = 1;
                  this.getDictionaryDetails();
                }
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  type: 'error',
                  message: '系统错误,我们正在殴打程序员'
                });
              })
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getDictionaryDetails();
			this.getDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>